package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.bean.BaseBean;
import com.taptech.doufu.bean.personalcenter.PersonalBaseAccount;

import java.util.List;

public class MessageListViewAdapter extends BaseListAdapter
{

	protected Context mContext;
	protected List<? extends BaseBean> mDataList;
	protected PersonalBaseAccount mAccount;
	protected BaseAdapterViewHolder[] viewHolders;
	protected BaseAdapterViewHolder viewHolder;
	protected BaseViewHolderFactory mViewHolderFactory;
	protected Handler messageHandler;


	public MessageListViewAdapter(Context mContext)
	{
		this.mContext = mContext;
	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, BaseAdapterViewHolder viewHolder)
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.viewHolder = viewHolder;
		this.mAccount = null;

	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, BaseAdapterViewHolder viewHolder , Handler handler)
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.viewHolder = viewHolder;
		this.mAccount = null;
		this.messageHandler = handler ;

	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, PersonalBaseAccount account, BaseAdapterViewHolder viewHolder )
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.mAccount = account;
		this.viewHolder = viewHolder;
		this.viewHolders = null;
	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, BaseViewHolderFactory viewHolderFactory)
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.mAccount = null;
		this.mViewHolderFactory = viewHolderFactory;
		this.viewHolders = new BaseAdapterViewHolder[viewHolderFactory.getItemTypeCount()];
	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, PersonalBaseAccount account, BaseViewHolderFactory viewHolderFactory)
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.mAccount = account;
		this.mViewHolderFactory = viewHolderFactory;
		this.viewHolders = new BaseAdapterViewHolder[viewHolderFactory.getItemTypeCount()];
	}

	public MessageListViewAdapter(Context mContext, List<? extends BaseBean> dataList, PersonalBaseAccount account, BaseViewHolderFactory viewHolderFactory,
								  Handler handler)
	{
		if (dataList == null)
		{
			return;
		}
		this.mContext = mContext;
		this.mDataList = dataList;
		this.mAccount = account;
		this.mViewHolderFactory = viewHolderFactory;
		this.viewHolders = new BaseAdapterViewHolder[viewHolderFactory.getItemTypeCount()];
		this.messageHandler = handler;
	}

	@Override
	public int getItemViewType(int position)
	{
		int viewType = -1;
		if (mViewHolderFactory != null && mDataList != null)
		{
			viewType = mViewHolderFactory.getViewHolderType(mDataList.get(position));
		}
		return viewType;
	}

	@Override
	public int getViewTypeCount()
	{
		int typeCount = 1;
		if (viewHolders != null)
		{
			typeCount = viewHolders.length;
		}
		return typeCount;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		int viewType = getItemViewType(position);

		TTLog.d("tag", "note list is ======" + viewType);
		if (convertView == null)
		{
			if (viewHolders != null && mDataList != null && viewType >= 0)
			{
				viewHolders[viewType] = mViewHolderFactory.createViewHolder(mContext, mDataList.get(position), position);
				viewHolders[viewType].setHandler(messageHandler);
				convertView = viewHolders[viewType].getConvertView();
				viewHolders[viewType].getChildView(convertView);
				convertView.setTag(viewHolders[viewType]);
			} else if (viewHolder != null)
			{
				viewHolder = viewHolder.createViewHolder(mContext, position);
				viewHolder.setHandler(messageHandler);
				convertView = viewHolder.getConvertView();
				viewHolder.getChildView(convertView);
				convertView.setTag(viewHolder);
			}

		} else
		{
			if (viewHolders != null)
			{
				viewHolders[viewType] = (BaseAdapterViewHolder) convertView.getTag();
			} else if (viewHolder != null)
			{
				viewHolder = (BaseAdapterViewHolder) convertView.getTag();
			}

		}
		if (position >= mDataList.size())
		{
			return convertView;

		}
		if (viewHolders != null && mDataList != null && viewType >= 0)
		{
			viewHolders[viewType].setChildView(mDataList.get(position),0);
		} else if (viewHolder != null)
		{
			viewHolder.setChildView(mDataList.get(position),0);
		}

		return convertView;
	}

	@Override
	public int getCount()
	{
		if (mDataList != null)
		{
			return mDataList.size();
		} else
			return 0;

	}

	@Override
	public Object getItem(int arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0)
	{
		// TODO Auto-generated method stub
		return 0;
	}


}
